package hk.service.impl;
import java.util.*;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Component;
import hk.entity.UserInfo;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		 // 正常应该到数据库里查询信息,为了方便,这里直接把数据写死了
        if(username.equals("admin")){
        	UserInfo userInfo = new UserInfo();
            userInfo.setName("admin");
            userInfo.setPassword("$2a$10$FWHmOGJVs/GNVqA.bPvnbu1au.oxKAJ7wK8bfbD6NpE5XbwdZtCpu"); //1234 对应的密文
            userInfo.setNote("备注"); 
            
			List<String> authorityList = Arrays
					.asList("ROLE_ADMIN","ROLE_USER","user:add","user:list","user:delete","js:delete");
            
            User userDetail=new User(userInfo.getName(),userInfo.getPassword(),AuthorityUtils.createAuthorityList(authorityList));
            return  userDetail; 
        }
        else{
	        throw new UsernameNotFoundException("用户名或密码错误");
	    }
	}
}